package cn.yunhe.spring.struts.controller;

import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

import com.opensymphony.xwork2.ActionContext;
import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import cn.yunhe.spring.mybatis.model.Page;
import cn.yunhe.spring.mybatis.model.User;
import cn.yunhe.spring.mybatis.service.IUserService;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 用户控制器
 * 
 * @author ZSL
 *
 */
public class UserAction extends ActionSupport implements ModelDriven<Object>{
	
	private static final long serialVersionUID = 1L;

	/**
	 * 注入用户业务服务
	 */
	@Autowired
	private IUserService userService;
	
	/**
	 * 用户对象参数
	 */
	private User user;
	/**
	 * 每页大小参数
	 */
	private int pageSize;
	/**
	 * 当前页码参数
	 */
	private int pageNum;
	/**
	 *返回分页数据对象
	 */
	private Page page;

	public void setUser(User user) {
		this.user = user;
	}
	public IUserService getUserService() {
		return userService;
	}
	public void setUserService(IUserService userService) {
		this.userService = userService;
	}
	public int getPageSize() {
		return pageSize;
	}
	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}
	public int getPageNum() {
		return pageNum;
	}
	public void setPageNum(int pageNum) {
		this.pageNum = pageNum;
	}
	public Page getPage() {
		return page;
	}
	public void setPage(Page page) {
		this.page = page;
	}
	
	/**
	 * 查询单个用户详细信息
	 * 
	 * @return
	 */
	public String findUser(){
		user = userService.getUser(user);
        ServletActionContext.getRequest().setAttribute("data", user);
		return "success";
	}
	
	/**
	 * 增加新用户
	 * 
	 * @return
	 */
	public String addUser(){
		userService.addUser(user);
		return "success";
	}
	
	/**
	 * 删除用户
	 * 
	 * @return
	 */
	public String delUser(){
		userService.delUser(user);
		return "success";
	}
	
	/**
	 * 编辑用户
	 * 
	 * @return
	 */
	public String editUser(){
		userService.editUser(user);
		return "success";
	}
	
	/**
	 * 模糊查询用户列表
	 * 
	 * @return
	 */
	public String queryLikeUsers(){
		Map<String,Object> cond=new HashMap<String,Object>();
		cond.put("pageSize", pageSize);
		cond.put("pageNum", pageNum);
		cond.put("user", user);
		page = userService.queryLikeUsers(cond);
		return "success";
	}

    /**
	 * 用户登录
	 * */
    public String login() throws Exception{
    	//创建一个User对象

		HttpServletRequest requset=ServletActionContext.getRequest();
    	String name=requset.getParameter("name");
    	String password=requset.getParameter("password");
        //得到out对象在前端页面上打印
		PrintWriter out=ServletActionContext.getResponse().getWriter();
		User login=userService.getUser(name,password);
    	if(name==null||password==null){
    		LOG.info("用户名或者密码不能为空！");
    		return "nologin";
		}else{
    		//如果登陆成功则在会话拦截器中获得login对象，进行判断登陆
		requset.getSession().setAttribute("login",login);
    	}
    	//userService.getUser(name,password);
    	return SUCCESS;
	}
	public User getModel() {
		if(user==null){
			user = new User();
		}
		return user;
	}
	
}
